package com.example.springboot.controller;

import cn.hutool.core.util.StrUtil;
import com.example.springboot.common.Result;
import com.example.springboot.entity.User;
import com.example.springboot.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@CrossOrigin
@RestController
public class UserController {
    @Resource
    UserService userService;
    @GetMapping("/userAll")
    List<User> findAll(){
        return userService.findAll();
    }
    @PutMapping("/update")
    public ResponseEntity<?> update(@RequestBody User user) {
        try {
            int result = userService.update(user);
            if (result == 1) {
                return ResponseEntity.ok(new ResponseData("200", "Update successful", user));
            } else {
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                        .body(new ResponseData("500", "Update failed", null));
            }
        } catch (Exception e) {
            // 打印日志
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(new ResponseData("500", "Internal server error", e.getMessage()));
        }
    }
}
